<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>水趣多</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html, body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
        #loading {
            width: 100%;
            text-align: center;
            color: #fff;
            font-size: 24px;
            vertical-align: middle;
            position: fixed;
            left: 0;
            top: 45%;
        }
        @font-face{
            font-family: "huakang";
            src: url("resource/assets/font/DFPHaiBaoW12-GB.ttf");
        }
    </style>
</head>

<body>
<div style="margin: auto;width: 100%;height: 100%;" class="egret-player"
     data-entry-class="Main"
     data-orientation="landscape"
     data-scale-mode="noBorder"
     data-frame-rate="30"
     data-content-width="1920"
     data-content-height="1080"
     data-multi-fingered="1"
     data-show-fps="false" data-show-log="false"
     data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
</div>

<div id="loading">
    <img src="images/loading.gif" alt="">
    <p></p>
</div>
<script>
    var load = 0;
    var destLoad = 0;
    var loadTimer = null;
    var loadEle = document.querySelector('#loading p');
    var clearLoadTimer = function(){
        if(loadTimer) {
            clearInterval(loadTimer);
            loadTimer = null;
        }
    }
    var showProgress = function(val, time, cb){
        clearLoadTimer();
        load = destLoad;
        var delta = val - load;
        destLoad = val;
        loadTimer = setInterval(()=>{
            load += delta / 10;
            if(load > 100 || load > val){
                if(load > 100){
                    load = 100;
                }else if(load > val){
                    load = val;
                }
                clearLoadTimer();
                if(cb){
                    cb();
                }
            }
            load = Math.floor(load);
            loadEle.innerHTML = "Loading......" + load + "%";
        }, time / 10);
    }
    var finishProgress = function(cb){
        if(load >= 100){
            hideLoading();
            cb();
        }else{
            showProgress(100,500,()=>{
                hideLoading();
                cb();
            });
        }
    }
    var hideLoading = function() {
        clearLoadTimer();
        var loading = document.querySelector('#loading');
        if(loading) {
            document.body.removeChild(loading);
        }
    }
    showProgress(20, 6000);
</script>
<script>
    var loadScript = function (list, callback) {
        var loaded = 0;
        var len = list.length;
        var loadNext = function () {
            loadSingleScript(list[loaded], function () {
                loaded++;
                var val = loaded / len * 30;
                if(val > 30){
                    val = 30;
                }
                val = Math.floor(val);
                showProgress(20+val,100);
                if (loaded >= list.length) {
                    callback();
                }
                else {
                    loadNext();
                }
            })
        };
        loadNext();
    };

    var loadSingleScript = function (src, callback) {
        var s = document.createElement('script');
        s.async = false;
        s.src = src;
        s.addEventListener('load', function () {
            s.parentNode.removeChild(s);
            s.removeEventListener('load', arguments.callee, false);
            callback();
        }, false);
        document.body.appendChild(s);
    };

    var xhr = new XMLHttpRequest();
    xhr.open('GET', './manifest.json?v=' + Math.random(), true);
    xhr.addEventListener("load", function () {
        var manifest = JSON.parse(xhr.response);
        var list = manifest.initial.concat(manifest.game);
        loadScript(list, function () {
            /**
             * {
             * "renderMode":, //Engine rendering mode, "canvas" or "webgl"
             * "audioType": 0 //Use the audio type, 0: default, 2: web audio, 3: audio
             * "antialias": //Whether the anti-aliasing is enabled in WebGL mode, true: on, false: off, defaults to false
             * "calculateCanvasScaleFactor": //a function return canvas scale factor
             * }
             **/
            egret.runEgret({ renderMode: "canvas", audioType: 0, calculateCanvasScaleFactor:function(context) {
                    var backingStore = context.backingStorePixelRatio ||
                        context.webkitBackingStorePixelRatio ||
                        context.mozBackingStorePixelRatio ||
                        context.msBackingStorePixelRatio ||
                        context.oBackingStorePixelRatio ||
                        context.backingStorePixelRatio || 1;
                    return (window.devicePixelRatio || 1) / backingStore;
                }});
        });
    });
    xhr.send(null);
</script>
<script>
    $isregister = {$data["isregister"]};
    $username = '{$data["username"]}';
    $mobile = '{$data["mobile"]}';
</script>
<script>
    // HTTP GET请求
    httpPost = function (url, data, callback) {
        var request = null;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest();
        } else {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (request) {
            request.onreadystatechange = function () {
                if (request.readyState == 4) {
                    if(request.status == 200){
                        callback(JSON.parse(request.responseText));
                    }else{
                        callback({"code":-1,"msg":"请求失败！"});
                    }
                }
            };
            request.open("POST", url, true);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset-UTF-8");
            request.send(data);
        }
    };
</script>
</body>

</html>
